package webModule.menu
{
	import com.greensock.TweenMax;
	
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	
	public class MenuItem extends Sprite
	{
		private var value:TextField;
		private var format:TextFormat;
		private var _index:int;
		private var _label:String;
		
		private var rectShape:Shape;
		
		public function get index():int{return this._index;}
		public function get label():String{return this._label;}
		public function MenuItem(label:String,index:int){
			this.buttonMode=true;
			this._index=index;
			this._label=label;
			
			
			rectShape=createRect();
			rectShape.scaleY=.1;
			addChild(rectShape);
			
			format=configFormat();
			value=new TextField();
			value.selectable=false;
			value.mouseEnabled=false;
			value.autoSize=TextFieldAutoSize.LEFT;
			
			value.multiline=false;
			value.defaultTextFormat=format;
			value.text=_label;
			value.y=30;
			value.x=this.width/2-value.width/2;
			addChild(value);
			drawBg();
			
			this.addEventListener(MouseEvent.MOUSE_OVER,changeState);
			this.addEventListener(MouseEvent.MOUSE_OUT,changeState);
			this.addEventListener(MouseEvent.CLICK,changeState);
		}
		private function changeState(evt:MouseEvent):void{
			switch(evt.type){
				case MouseEvent.MOUSE_OVER:
					TweenMax.to(rectShape,.1,{scaleY:1});
					format.color=0xffffff;
					value.setTextFormat(format);
					TweenMax.to(value,.1,{y:50});
					//TweenLite.to(this, ShowStyle.CONTINUANCE, {scaleX:ShowStyle.TOSCALE, ease:ShowStyle.OUT_STYLE})
					break;
				case  MouseEvent.MOUSE_OUT:
					TweenMax.to(rectShape,.1,{scaleY:.1});
					format.color=0x666666;
					value.setTextFormat(format);
					TweenMax.to(value,.1,{y:30});
					break;
				case MouseEvent.CLICK:
					this.dispatchEvent(new MenuEvent(MenuEvent.MENU_SELECTED,this));
					
			}
		}
		private function createRect():Shape{
			var rect:Shape=new Shape;
			var fillType:String =GradientType.LINEAR;
			var alphas:Array = [1, 1];
			var colors:Array=[0x4a9dd7,0x285473];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix=new Matrix();
			
			//matr.rotate(Math.PI/2);
			matr.createGradientBox(60, 80, Math.PI/2, 0, 0);
			var spreadMethod:String =SpreadMethod.PAD;
			rect.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);   
			rect.graphics.drawRect(0,0,60,80);
			return rect;
		}
		
		
		private function configFormat(col:uint=0x666666):TextFormat{
			var format:TextFormat=new TextFormat();
			format.align=TextFormatAlign.LEFT;
			format.size=12;
			format.color=col;
			
			return format;
		}
		
		private function drawBg():void{
			this.graphics.beginFill(0x000,0);
			this.graphics.drawRect(0,0,60,80);
			this.graphics.endFill();
		}
	}
}